/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2024-12-06
 * Time: 20:47
 */
class Solution4 {
    public String countAndSay(int n) {
        String ret = "1";

        //1:下面的循环用于依次解释ret,只用解释n-1次
        for(int i = 1 ; i <= n-1 ; i++){
            //每一次都是一个新的StringBuilder
            StringBuilder temp = new StringBuilder();
            //模拟：第一次解释，对应的是n=2
            int len = ret.length();
            for(int left = 0 , right = 0 ; right < len ; ){
                //2:模拟有几个相同的数
                while(right < len && ret.charAt(left) == ret.charAt(right)){
                    right++;
                }
                temp.append(Integer.toString(right-left));
                temp.append(ret.charAt(left));
                left = right;
            }
            //更新ret,StringBuilder转换为String类型
            ret = temp.toString();
        }
        return ret;
    }
}
public class Test4 {
}
